//
//  GravityViewController.m
//  SDKSample
//
//  Created by 小肥羊 on 15/12/15.
//  Copyright © 2015年 Zhu‘s Game House. All rights reserved.
//

#import "GravityViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface GravityViewController ()

@property (nonatomic, strong) CMMotionManager *mManager;
@property (weak, nonatomic) IBOutlet UILabel *xLabel;
@property (weak, nonatomic) IBOutlet UILabel *yLabel;
@property (weak, nonatomic) IBOutlet UILabel *zLabel;

@end

@implementation GravityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mManager = [[CMMotionManager alloc] init];
    
    /* 设置采样的频率，单位是秒 */
    NSTimeInterval updateInterval = 0.1;
    
    /* 判断是否加速度传感器可用，如果可用则继续 */
    if ([self.mManager isAccelerometerAvailable] == YES) {
        /* 给采样频率赋值，单位是秒 */
        [self.mManager setAccelerometerUpdateInterval:updateInterval];
        
        GravityViewController *__weak weakSelf = self;
        /* 加速度传感器开始采样，每次采样结果在block中处理 */
        [self.mManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
         {
             [weakSelf.xLabel setText:[NSString stringWithFormat:@"%@",@(accelerometerData.acceleration.x)]];
             [weakSelf.yLabel setText:[NSString stringWithFormat:@"%@",@(accelerometerData.acceleration.y)]];
             [weakSelf.zLabel setText:[NSString stringWithFormat:@"%@",@(accelerometerData.acceleration.z)]];
         }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
